package com.study.webapp.jdk8;


public class ThreadOrder {

    private volatile  int flag = 1;


    public void first (String str) throws InterruptedException{
        while (flag % 3 != 1) {

        }
        System.out.println(str);
        flag++;
    }

    public void second(String str) throws InterruptedException {
        while (flag % 3 != 2) {

        }
        System.out.println(str);
        flag++;
    }
    public void third(String str) throws InterruptedException {
        while (flag % 3 != 0) {

        }
        System.out.println(str);
        System.out.println("==============");
        flag++;
    }

    public static void main(String[] args) {
        ThreadOrder threadOrder = new ThreadOrder();
        new Thread(() ->{
            try {
                for (int i = 0; i<10; i++) {
                    Thread.sleep(500);
                    threadOrder.second("B");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() ->{
            try {
                for (int i = 0; i<10; i++) {
                    threadOrder.first("A");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() ->{
            try {
                for (int i = 0; i<10; i++) {
                    threadOrder.third("C");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}

